<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Index extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }
    
    // ------------------------------------------------------------------------
    
    public function index()
    {
        $this->auth->is_logged_in();
        
        redirect('dashboard');
    }
    
    // ------------------------------------------------------------------------
    
    public function login()
    {
        $u = $this->auth_model->get_user();
		
		if ($u) redirect('/');
		
		if ($this->input->is_ajax_request())
        {
            $this->form_validation->set_rules('email', '<strong>Email</strong>', 'required|valid_email');
            $this->form_validation->set_rules('password', '<strong>Password</strong>', 'required|trim');
            
            if ($this->form_validation->run())
            {
                $post = array(
                    'email'     => $this->input->post('email', TRUE),
                    'password'  => $this->input->post('password', TRUE)
                );
                
                $query = $this->auth->login($post);
				
				return $query ? $this->response(TRUE, 'redirect', site_url('dashboard')) : $this->response(FALSE, 'errors', '<p class="error">Email Address or Password is incorrect.</p>');
            }
            
			$this->response(FALSE, 'errors', validation_errors());
        }
		
		// load view
		
		$data['title'] = 'Login';
		$this->load->view('login', $data);
    }
    
    // ------------------------------------------------------------------------
	
	public function logout()
	{
		$this->auth->logout();
		redirect('login');
	}
	
	// ------------------------------------------------------------------------
}

/* End of file admin.php */
/* Location: ./application/controllers/admin.php */